﻿<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >搜索</el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-plus"
        :disabled="!checkPermission(['AbpTenantManagement.Tenants.Create'])"
        @click="handleShowCreateOrEditTenantDialog('')"
      >
        添加
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      style="width: 100%"
      :data="list"
      @sort-change="sortChange"
    >
      <el-table-column label="租户" width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="创建时间" width="210px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.creationTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" width="210px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.lastModificationTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        width="230"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{row}">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            :disabled="!checkPermission(['AbpTenantManagement.Tenants.Update'])"
            @click="handleShowCreateOrEditTenantDialog(row.id)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            :disabled="!checkPermission(['AbpTenantManagement.Tenants.Delete'])"
            @click="handleDelete(row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <tenant-create-or-edit-form
      :show-dialog="showCreateOrEditTenantDialog"
      :tenant-id="editTenantId"
      @closed="handleCreateOrEditTenantFormClosed"
    />
  </div>
</template>

<script>
import { getListPaged, remove } from '@/api/tenant-management'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import checkPermission from '@/utils/permission' // 权限判断函数
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import TenantCreateOrEditForm from './components/TenantCreateOrEditForm'

export default {
  name: 'Tenants',
  components: { Pagination, TenantCreateOrEditForm },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined,
        sorting: 'creationTime'
      },
      dialogFormVisible: false,
      downloadLoading: false,
      loading: false,
      showCreateOrEditTenantDialog: false,
      showEditTenantConnectionDialog: false,
      editTenantId: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission,
    getList() {
      this.listLoading = true
      const params = {
        filter: this.listQuery.name,
        sorting: this.listQuery.sorting,
        skipCount: (this.listQuery.page - 1) * this.listQuery.limit,
        maxResultCount: this.listQuery.limit
      }
      getListPaged(params).then(response => {
        this.list = response.data.items
        this.total = response.data.totalCount
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'creationTime') {
        this.sortByCreationTime(order)
      }
    },
    sortByCreationTime(order) {
      if (order === 'ascending') {
        this.listQuery.sorting = 'creationTime'
      } else {
        this.listQuery.sorting = 'creationTime desc'
      }
      this.handleFilter()
    },
    handleDelete(row) {
      this.$confirm('此操作将删除该, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        const { id } = row
        remove(id).then(() => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      }).catch(() => {
      })
    },
    handleShowCreateOrEditTenantDialog(id) {
      this.editTenantId = id
      this.showCreateOrEditTenantDialog = true
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
        const filterVal = ['timestamp', 'title', 'type', 'importance', 'status']
        const data = this.formatJson(filterVal, this.list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'table-list'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}`
        ? 'ascending'
        : sort === `-${key}`
          ? 'descending'
          : ''
    },
    handleCreateOrEditTenantFormClosed(changed) {
      this.showCreateOrEditTenantDialog = false
      this.editTenantId = ''
      if (changed) {
        this.getList()
      }
    }
  }
}
</script>
